package com.xdtech.trans.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.xdtech.sys.aspect.SystemControllerLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.trans.service.TransAccountTradeService;
import com.xdtech.trans.vo.TransAccountItem;
import com.xdtech.trans.vo.TransAccountTradeItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-06-07 14:39:35
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TransAccountTradeController {
	private Log log = LogFactory.getLog(TransAccountTradeController.class);
	@Autowired
	private TransAccountTradeService transAccountTradeService;
	@RequestMapping(value="/transAccountTrade.do",params = "transAccountTrade")
	public ModelAndView skipTransAccountTrade() {
		return new ModelAndView("trans/transAccountTrade/transAccountTrade_ftl");
	}
	
	@RequestMapping(value="/transAccountTrade.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(TransAccountTradeItem item,Pagination pg) {
		Map<String, Object> results =  transAccountTradeService.loadPageDataByConditions(pg,item,"findTransAccountTradeByCondition");
		return results;
	}
	
	@RequestMapping(value="/transAccountTrade.do",params = "editTransAccountTrade")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editTransAccountTrade(HttpServletRequest request,Long transAccountTradeId) {
		if (transAccountTradeId!=null) {
			request.setAttribute("transAccountTradeItem", transAccountTradeService.loadTransAccountTradeItem(transAccountTradeId));
		}
		return new ModelAndView("trans/transAccountTrade/editTransAccountTrade_ftl");
	}
	
	@RequestMapping(value="/transAccountTrade.do",params = "saveTransAccountTrade")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveTransAccountTrade(TransAccountTradeItem item) {
		ResultMessage r = new ResultMessage();
		if (transAccountTradeService.saveOrUpdateTransAccountTrade(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/transAccountTrade.do",params = "deleteTransAccountTradeItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteTransAccountTradeItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> transAccountTradeIds = new ArrayList<Long>();
			for (String id : tempIds) {
				transAccountTradeIds.add(Long.valueOf(id));
			}
			transAccountTradeService.deleteTransAccountTradeInfo(transAccountTradeIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/transAccountTrade.do",params = "inTransAccount")
	@ResponseBody
	@SystemControllerLog(description = "资金入账")
	public ResultMessage inTransAccount(TransAccountTradeItem item) {
		ResultMessage r = transAccountTradeService.inTransAccount(item);
		return r;
	}

	@RequestMapping(value="/transAccountTrade.do",params = "outTransAccount")
	@ResponseBody
	@SystemControllerLog(description = "资金提取")
	public ResultMessage outTransAccount(TransAccountTradeItem item) {
		ResultMessage r = transAccountTradeService.outTransAccount(item);
		return r;
	}
	
	
}
